package com.ddwl.goods.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.goods.service.CategoryService;
import com.ddwl.goods.service.MallCategoryService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.CategoryBo;
import com.ddwl.schema.bo.goods.CategorySearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/7/11 18:45
 */
@Api(tags = "分类管理")
@RequestMapping("category")
@RestController
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MallCategoryService mallCategoryService;

    @PostMapping("page")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("分页查询分类")
    public RestVo<PageVo<CategoryVo>> page(@Valid @RequestBody PageBo<CategorySearchBo> pageBo) {
        pageBo.getParam().setCategoryType(GoodsEnum.CategoryTypeEnum.PLATFORM.getCode());
        return RestVo.ok(categoryService.page(pageBo));
    }

    @PostMapping("tree")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.API})
    @ApiOperation("树型结构")
    public RestVo<List<CategoryVo>> tree(@RequestBody CategorySearchBo bo) {
        bo.setCategoryType(GoodsEnum.CategoryTypeEnum.PLATFORM.getCode());
        return RestVo.ok(categoryService.tree(bo));
    }

    @PostMapping("index")
    @Auth(required = false, product = {BaseEnum.ProductEnum.ZHENYU, BaseEnum.ProductEnum.CLOTHES, BaseEnum.ProductEnum.MALL})
    @ApiOperation("首页分类")
    public RestVo<List<CategoryVo>> index(@RequestBody CategorySearchBo bo) {
        bo.setProduct(getProduct());
        bo.setCategoryType(GoodsEnum.CategoryTypeEnum.PLATFORM.getCode());
        return RestVo.ok(categoryService.index(bo));
    }

    @PostMapping("/mallIndex")
    @Auth(required = false, product = {BaseEnum.ProductEnum.ZHENYU, BaseEnum.ProductEnum.CLOTHES, BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.IOT,BaseEnum.ProductEnum.TERMINAL,BaseEnum.ProductEnum.FENXIAO})
    @ApiOperation("商城首页一级分类")
    public RestVo<List<CategoryVo>> mallIndex() {
        return RestVo.ok(mallCategoryService.indexMall());
    }

    @PostMapping("/mallChildren")
    @Auth(required = false, product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    @ApiOperation("商城分类子目录")
    public RestVo<List<CategoryVo>> mallChildren(@RequestBody CategorySearchBo bo) {
        return RestVo.ok(mallCategoryService.searchMallChildren(bo));
    }

    @GetMapping("/wbmIndex")
    @Auth(required = false, product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马发现页分类")
    public RestVo<List<CategoryVo>> wbmIndex() {
        return RestVo.ok(categoryService.wbmIndex());
    }

    @PostMapping("/create")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("创建分类")
    public RestVo<String> create(@Valid @RequestBody CategoryBo bo) {
        return RestVo.ok(categoryService.create(bo));
    }

    @PostMapping("/update")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("修改分类")
    public RestVo update(@Valid @RequestBody CategoryBo bo) {
        categoryService.update(bo);
        return RestVo.ok();
    }

    @DeleteMapping("del")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("删除分类")
    public RestVo delete(@ApiParam(value = "分类ID", required = true) @RequestParam String id) {
        categoryService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("分类详情")
    public RestVo<CategoryVo> detail(@ApiParam(value = "分类ID", required = true) @RequestParam String id) {
        return RestVo.ok(categoryService.detail(id));
    }

    @PostMapping("/changeCategoryCode")
    @ApiOperation("把id反转成时间戳")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo changeId(){
        categoryService.changeCategoryCode();
        return RestVo.ok();
    }

}
